//
//  AlbumListCollectionViewController.swift
//  aj
//
//  Created by leeqb on 3/1/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class AlbumListCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    var addFlag = false
    var editFlag = true
    var cellSize: CGSize!
    var albumListData: [NSDictionary]? {
        didSet {
            self.collectionView?.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView?.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        let addImage = UIImage(named: "album_add.png")
        let cellWidth = (SCREEN_WIDTH - 30) / 2
        let scale = cellWidth / (addImage?.size.width)!
        let cellHeight = (addImage?.size.height)! * scale + 26
        cellSize = CGSize(width: cellWidth, height: cellHeight)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return cellSize
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let rowCount = albumListData?.count ?? 0
        if addFlag {
            return rowCount + 1
        } else {
            return rowCount
        }
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("AlbumCell", forIndexPath: indexPath)
        
        if addFlag && (albumListData == nil || (indexPath.row == albumListData!.count)) {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier("AlbumAddCell", forIndexPath: indexPath)
        } else {
            let rowData = albumListData![indexPath.row]
            
            let imageView = cell.viewWithTag(2) as! UIImageView
            let checkImageView = cell.viewWithTag(3) as! UIImageView
            let textLabel = cell.viewWithTag(4) as! UILabel
            
            let defaultImage = UIImage(named: "album_no_photo.png")
            imageView.sd_setImageWithURL(NSURL(string: (rowData["zoneUrl"] as? String ?? "")), placeholderImage: defaultImage)
            textLabel.text = "\((rowData["zoneName"])!)(\((rowData["photoNumber"])!))张"
            
            if rowData["checked"] == nil || (rowData["checked"] as! Bool) == false {
                checkImageView.image = UIImage(named: "spc_uncheck.png")
            } else {
                checkImageView.image = UIImage(named: "spc_checked.png")
            }
            
            if editFlag {
                checkImageView.hidden = false
            } else {
                checkImageView.hidden = true
            }
        }
        
        let outView = cell.viewWithTag(1)!
        outView.layer.borderColor = UIColor.lightGrayColor().CGColor
        
        if cell.selectedBackgroundView == nil {
            let backgroundView = UIView()
            backgroundView.backgroundColor = UIColor.orangeColor()
            cell.selectedBackgroundView = backgroundView
        }
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
    }
}
